#include "alterwidget.h"

AlterWidget::AlterWidget(QWidget *parent) : QWidget(parent)
{
    init_Widget();
    init_Control();
    init_Connect();
}

void AlterWidget::init_Control()
{
    this->idLab=new QLabel();
    this->idLab->setText(QString::fromUtf8("请输入修改用户的id："));

    this->idEt=new QLineEdit();
    this->confirmEt=new QLineEdit();
    this->confirmEt->setPlaceholderText("请输入修改后的数据");

    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));
    this->confirmBtn=new QPushButton();
    this->confirmBtn->setText(QString::fromUtf8("确认"));


    this->confirmCombo=new QComboBox();
    this->confirmCombo->addItem("账号");
    this->confirmCombo->addItem("姓名");
    this->confirmCombo->addItem("密码");
    this->confirmCombo->addItem("身份");

    this->btnhLay=new QHBoxLayout();
    this->btnhLay->addWidget(this->confirmBtn);
    this->btnhLay->addWidget(this->returnBtn);

    this->boxhLay=new QHBoxLayout();
    this->boxhLay->addWidget(this->confirmCombo);
    this->boxhLay->addWidget(this->confirmEt);

    this->otherhLay=new QHBoxLayout();
    this->otherhLay->addWidget(this->idLab);
    this->otherhLay->addWidget(this->idEt);

    this->vLay=new QVBoxLayout();
    this->vLay->addLayout(this->otherhLay);
    this->vLay->addLayout(this->boxhLay);
    this->vLay->addLayout(this->btnhLay);
    this->vLay->setContentsMargins(100,50,100,50);

    this->setLayout(this->vLay);
}

void AlterWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("修改用户信息"));
    this->resize(800,600);
}

void AlterWidget::init_Connect()
{
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
    connect(this->confirmBtn,SIGNAL(clicked()),this,SLOT(on_confirmBtn_cliked()));
    connect(confirmCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_Index_Changed(int)));
}

QString AlterWidget::searchId()
{
    //确认id是否存在
    QString id;user=nullptr;
    id=this->idEt->text();
    QString sql=QString("SELECT *FROM Users WHERE user_id='%1';").arg(id);
    this->model=this->control->getInstance()->Creatmodel("user");
    this->user=this->model->getUserByName((char*)sql.toStdString().c_str());
    if(user)
    {
        return id;
    }
    else
    {
        return nullptr;
    }

}

int AlterWidget::userAlter()
{
    //实现修改操作
    int ture;
    QString changeId,changeInfo,id,sql;
    changeInfo=this->confirmEt->text();
    if(this->index==0)
    {
        changeId="user_name";
    }
    else if (this->index==1)
    {
        changeId="Name";
    }
    else if (this->index==2)
    {
        changeId="user_password";
    }
    else
    {
         changeId="user_jur";
    }
    id=searchId();
    if(id!=nullptr)
    {
        sql=QString("UPDATE Users SET %1='%2' WHERE user_id='%3';").arg(changeId).arg(changeInfo).arg(id);
        qDebug()<<sql;

        this->model=this->control->getInstance()->Creatmodel("user");
        ture=this->model->Table_Inserter((char*)sql.toStdString().c_str());
        if(ture)
        {
             QMessageBox::information(this,"成功","修改成功");
             this->confirmEt->clear();
             this->idEt->clear();
        }
        else
        {
            QMessageBox::critical(this, "错误", "修改失败");
            this->confirmEt->clear();
            this->idEt->clear();
            return 0;
        }

    }
    else
    {
        QMessageBox::critical(this, "错误", "没有该用户");
        this->confirmEt->clear();
        this->idEt->clear();
        return 0;
    }
    return 1;

}

void AlterWidget::on_retutrnBtn_cliked()
{
    //qDebug()<<"发出信号";
    emit returnManager();
}

void AlterWidget::on_confirmBtn_cliked()
{
    if(userAlter())
    emit returnManager();
}

void AlterWidget::on_Index_Changed(int index)
{
    //查看修改什么
    this->index=index;
}
